package org.goldfish.chatroom.controller;

import org.goldfish.chatroom.entity.VerificationMessage;
import org.goldfish.chatroom.mapper.VerificationMessageMapper;
import org.goldfish.chatroom.service.MailVerificationServiceManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/verify")
public class VerificationController {
    @Autowired
    private VerificationMessageMapper verificationMessageMapper;

    @Autowired
    private MailVerificationServiceManager mailVerificationServiceManager;

    @GetMapping
    public String verify(
            Model model
    ) {
        VerificationMessage message = verificationMessageMapper.getLatestVerificationMessage(true);
        mailVerificationServiceManager.sendVerificationMessage(message);
        String receiveAddr = mailVerificationServiceManager.getReceiveAddress();
        String receiveProvider = mailVerificationServiceManager.translate(receiveAddr);
        model.addAttribute("site", mailVerificationServiceManager.getReceiveAddress());
        model.addAttribute("link", receiveProvider);
        model.addAttribute("time", message.getTime());
        return "verify";
    }

}
